package com.ctl.szmd.szmd.feature_wm.ui.adapter.p1

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import com.ctl.lib_common.base.mvvm.DataBindingViewHolder
import com.ctl.szmd.szmd.feature_wm.R
import com.ctl.szmd.szmd.feature_wm.data.bean.FulianSubOrgInfoForListForApiList
import com.ctl.szmd.szmd.feature_wm.databinding.AdapterSubOrgInfoBinding
import com.ctl.szmd.szmd.utils.ext.doWithTry

/**
 * created by : chentl
 * Date: 2020/08/17
 */
class SubOrgInfoAdapter :
    PagingDataAdapter<FulianSubOrgInfoForListForApiList, SubOrgInfoAdapter.MyViewHolder>(
        FulianSubOrgInfoForListForApiList.diffCallback
    ) {

    class MyViewHolder(view: View) :
        DataBindingViewHolder<FulianSubOrgInfoForListForApiList>(view) {
        val mBinding: AdapterSubOrgInfoBinding by viewHolderBinding(view)
        override fun bindData(data: FulianSubOrgInfoForListForApiList, position: Int) {
            mBinding.apply {
                model = data
                executePendingBindings()
            }
            itemView.tag = data
        }
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        doWithTry {
            val data = getItem(position)
            data?.let {
                holder.bindData(data, position)
            }
        }
    }
    var listener: View.OnClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.adapter_sub_org_info, parent, false)
        val holder = MyViewHolder(view)
        holder.itemView.setOnClickListener {
            listener?.onClick(it)
        }
        return holder
    }
}